package instantHelper.Raceway;

import instantHelper.Models.ConnectingRequests;
import java.io.*;
import java.util.*;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpParams;
import android.app.Activity;
import android.content.*;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.*;
import android.provider.Settings;
import android.provider.Settings.Secure;
import android.provider.Settings.SettingNotFoundException;
import android.util.Log;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.*;

public class ClosedMode extends Activity {

	DbHelper myDBHelper;
	private String TAG = ClosedMode.class.getSimpleName();
	private int requestCount = 0;
	private int requestCountForGPS = 0;
	int pictureCounter = 0;
	HashMap<String, String> settings = new HashMap<String, String>(5);

	String picturePath = "";
	String androidId = "";
	Button buttonClick;
	private double latitude;
	private double longtitude;
	boolean isSendGPS;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.closed_mode);

		/* Use the LocationManager class to obtain GPS locations */
		LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		LocationListener mlocListener = new MyLocationListener();
		mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
				mlocListener);
		androidId = SettingsUtil.getAndroidID(getApplicationContext());

		// This method for closing the screen light
		handleScreenLightDark();
		sendHelpRequestToCenterAPP();
		// preview = new Preview(this);

		// ((FrameLayout) findViewById(R.id.preview)).addView(preview);

		try {
			sendingStart();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void sendHelpRequestToCenterAPP() {
		String url = InstantHelperConstant.CentralAppURL
				+ "/androidhelprequest/newHelpRequest";

		String requestID = "";// request insert on centerapp
		String requestURI = androidId + "_" + requestID;

		// List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
		// nameValuePairs.add(new BasicNameValuePair("requestURI", requestURI));
		// nameValuePairs.add(new BasicNameValuePair("phoneId", androidId));
		//
		// ConnectingRequests connectingRequests = new ConnectingRequests(url,
		// nameValuePairs);
		// connectingRequests.postData();
	}

	protected void sendingStart() throws InterruptedException {
		isSendGPS = true;
		Object ProfileName = SettingsUtil
				.getProfileName(getApplicationContext());
		if (ProfileName == "Audio") {
			try {
				AudioRecorder audioRecorder = new AudioRecorder(
						SettingsUtil.getAndroidID(getApplicationContext()),
						requestCount);
				audioRecorder.recordAndSendAudio();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else if (ProfileName == "Video") {

		} else if (ProfileName == "Picture") {
			takePictureAndSend();
		}
		if (settings.get("sendGPS").equals("0")) {
			isSendGPS = true;
		}
		requestCountForGPS = requestCount;
		requestCount++;
	}

	// FOR TAKE PICTURE
	protected void takePictureAndSend() {
		// preview.setAndroidId(androidId);
		// preview.setRequestCount(requestCount);
		// preview.isClosedMode(true);
	}

	private String sanitizePicturePath(String path) {
		if (!path.startsWith("/")) {
			path = "/" + path;
		}
		if (!path.contains(".")) {
			path += ".jpg";
		}

		return Environment.getExternalStorageDirectory().getAbsolutePath()
				+ path;
	}

	/* Class My Location Listener */
	// For sending GPS

	public class MyLocationListener implements LocationListener {
		@Override
		public void onLocationChanged(Location loc) {
			latitude = loc.getLatitude();
			longtitude = loc.getLongitude();
			if (isSendGPS) {
				sendGPSToCentralAPP(latitude, longtitude);
			}
		}

		@Override
		public void onProviderDisabled(String provider) {
		}

		@Override
		public void onProviderEnabled(String provider) {
		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
		}
	}/* End of Class MyLocationListener */

	// For sending GPS
	public void sendGPSToCentralAPP(double latitude2, double longtitude2) {
		String url = InstantHelperConstant.CentralAppURL
				+ "/androidhelprequest/newGPSData";

		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(7);
		nameValuePairs.add(new BasicNameValuePair("gpsx", Double
				.toString(latitude2)));
		nameValuePairs.add(new BasicNameValuePair("gpsy", Double
				.toString(longtitude2)));
		nameValuePairs.add(new BasicNameValuePair("androidId", androidId));
		nameValuePairs.add(new BasicNameValuePair("requestCount", Integer
				.toString(requestCountForGPS)));
		ConnectingRequests connectingRequests = new ConnectingRequests(url,
				nameValuePairs);
		connectingRequests.postData();
	}

	private void handleScreenLightDark() {
		PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
		PowerManager.WakeLock wl = pm.newWakeLock(
				PowerManager.PARTIAL_WAKE_LOCK, TAG);
		wl.acquire();
		// This is another screen brightness code
		int brightnessMode = 0;
		try {
			brightnessMode = Settings.System.getInt(getContentResolver(),
					Settings.System.SCREEN_BRIGHTNESS_MODE);
		} catch (SettingNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		Settings.System.putInt(getContentResolver(),
				Settings.System.SCREEN_BRIGHTNESS_MODE,
				Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
		android.provider.Settings.System.putInt(getContentResolver(),
				android.provider.Settings.System.SCREEN_BRIGHTNESS, 10);

		WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
		layoutParams.screenBrightness = 0.1F;
		layoutParams.buttonBrightness = 0.1F;
		getWindow().setAttributes(layoutParams);
	}

}
